/**
 * Created by Administrator on 2016/7/25.
 */
swan_pc
    .controller("friendManageController",function($scope,$q,friendManage,getMyFriendById,friendManageUpdate,messageManage,GetAccountInfo,getUserByLogin){
        //新加好友功能
        $scope.addFriendMessage={
            "parameter":null,
            "type": "ADD_FRIEND"
        }
        $scope.applyFriendTiming="";
        $scope.ApplyFriend=function(){
            $scope.parameterString='{"friendTiming":"'+ $scope.applyFriendTiming +'"}';
            $scope.addFriendMessage.parameter=$scope.parameterString;
            console.log($scope.addFriendMessage);
            messageManage.save($scope.addFriendMessage,function(result){
                console.log(result);
                swal("好友申请发送成功","请您耐心等待好友回复","success");
            },function(result){
                console.log(result);
                swal("好友申请发送失败","请检查您的账号填写是否有误","error");
            })
        }
        //permission展示
        $scope.permissionDisplay=function(option){
            var permissionString="";
            if(option==true){
                permissionString="有";
            }else{
                permissionString="无";
            }
            return permissionString;
        }
        //权限管理模块
        $scope.addfriendPermission=
        {
            "editPermission": false,
            "friendTiming": null,
            "invitePermission": false,
            "viewPermission": false
        };
        $scope.putfriendPermission={
            "id":0,
            "editPermission": false,
            "friendTiming": null,
            "invitePermission": false,
            "viewPermission": false,
            "userTiming":null,
            "friendId":0,
            "userId":0
        }
        $scope.inputFriendPermission=null;
        $scope.changeFriendPermission="";
        $scope.myFriendName="";
        $scope.myFriend=[];
        $scope.getMyAuthorizeFriend=function(){
            friendManage.query(function(result){
                    $scope.myFriend=result;
                    //console.log("success:"+$scope.myFriend);
                },
                function(result){
                    console.log(result);
                })
        };
        $scope.getMyAuthorizeFriend();
        $scope.editFriendPermission=function(id,per){
            var def1= $q.defer();
            var promise1=def1.promise;
            getMyFriendById.query({fpid:id},function(result){
                $scope.updatefriendPermission=result;
                //console.log("get:");
                //console.log($scope.updatefriendPermission);
                def1.resolve();
            },
            function(result){
                def1.reject();
            });
            promise1.then(
                function() {
                    if(per=="invite"){
                        $scope.putfriendPermission.invitePermission=true;
                        $scope.putfriendPermission.viewPermission=false;
                        $scope.putfriendPermission.editPermission=false;
                    }else if(per=="view"){
                        $scope.putfriendPermission.invitePermission=true;
                        $scope.putfriendPermission.viewPermission=true;
                        $scope.putfriendPermission.editPermission=false;
                    }else{
                        $scope.putfriendPermission.invitePermission=true;
                        $scope.putfriendPermission.viewPermission=true;
                        $scope.putfriendPermission.editPermission=true;
                    }
                    $scope.putfriendPermission.id=$scope.updatefriendPermission.id;
                    $scope.putfriendPermission.userTiming=$scope.updatefriendPermission.userTiming;
                    $scope.putfriendPermission.userId=$scope.updatefriendPermission.userId;
                    $scope.putfriendPermission.friendTiming=$scope.updatefriendPermission.friendTiming;
                    $scope.putfriendPermission.friendId=$scope.updatefriendPermission.friendId;
                    friendManageUpdate.update({},$scope.putfriendPermission,function (result) {
                            $scope.getMyAuthorizeFriend();
                            swal("更新成功", $scope.updatefriendPermission.friendTiming, "success");
                        },
                        function (result) {
                            swal("更新失败", $scope.updatefriendPermission.friendTiming, "error");
                        })
                }
            )
        }
        $scope.deleteFriendPermission=function(id){
            var def=$q.defer();
            var promise=def.promise;
            getMyFriendById.query({fpid:id},function(result){
                $scope.updatefriendPermission=result;
                def.resolve();
            },
            function(result){
                def.reject();
            });
            promise.then(
                getMyFriendById.delete({fpid:id},function(result){
                    $scope.getMyAuthorizeFriend();
                    swal("删除成功",$scope.updatefriendPermission.friendTiming,"success");
                },
                function(result){
                    console.log(result);
                    swal("删除失败",$scope.updatefriendPermission.friendTiming,"error");
                })
            )
        }
    })